package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateUserFeedbackPageDTO;
import com.itlong.cloud.POJO.PO.FeedbackInfoPO;
import com.itlong.cloud.POJO.VO.operate.OperateUserFeedbackVO;
import com.itlong.cloud.operate.dao.IOperateUserFeedbackDao;
import com.itlong.cloud.operate.service.IOperateUserFeedbackService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营—APP用户-用户反馈服务接口实现类。
 * <desc/>
 *
 * @createDate 2017/9/22.
 */
@Service
public class OperateUserFeedbackServiceImpl implements IOperateUserFeedbackService {

    private static final Logger LOG = LoggerFactory.getLogger(OperateUserManageServiceImpl.class);

    @Autowired
    IOperateUserFeedbackDao iOperateUserFeedbackDao;


    /**
     * <desc>
     *      反馈用户信息回复。
     * <desc/>
     *
     * @param id
     * @param replyContent 回复内容
     * @return
     * @author shengen.T
     * @createDate 2017/9/25
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer replyInfo(String id, String replyContent, Integer clientShowResult) {
        return iOperateUserFeedbackDao.replyContent(id, replyContent, clientShowResult);
    }

    /**
     * <desc>
     *      分页获取条件查询用户列表数据。
     * <desc/>
     *
     * @param pageDTO 请求参数
     * @return 返回分页数据
     * @author shengen.T
     * @createDate 2017/9/25
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateUserFeedbackVO> getPage(OperateUserFeedbackPageDTO pageDTO) throws Exception {
        //将DTO转换为Map params
        Map<String, Object> params = null;
        params = SqlUtil.convertBeanToMapParams(pageDTO);
        //分页类默认pageSize为20
        Page<OperateUserFeedbackVO> page = new Page<>();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateUserFeedbackDao.getFeedbackUserList(params), iOperateUserFeedbackDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      处理app用户反馈主键为空的数据
     * </desc>
     *
     * @Author Zhu.yj
     * @createDate  2020/11/27
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer handlerFeedbackIdIsNull() throws Exception {
        List<Integer> idList = iOperateUserFeedbackDao.getFeedbackIdIsNullList();
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Integer id : idList){
            Map<String, Object> params = new HashMap<>();
            params.put("id", id);
            params.put("feedbackId", LogicIdUtil.bussinessId());
            mapList.add(params);
        }
        if ( !mapList.isEmpty()){
            return iOperateUserFeedbackDao.updateFeedbackIdIsNull(mapList);
        }
        return 0;
    }
}
